package com.ktjy.t382.ch01.service;

import com.ktjy.t382.ch01.entity.SysUser;
import com.ktjy.t382.ch01.entity.SysUsers;
import com.ktjy.t382.ch01.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysUserServiceImp implements SysUserService {

    @Resource
    SysUserMapper sysUserMapper;

    @Cacheable(value = "user", keyGenerator = "keyGenerator")
    //@CacheEvict(value = "user", keyGenerator = "keyGenerator")
    @Override
    public SysUser getSysUserByIdService(SysUser sysUser) {
        return sysUserMapper.getSysUserById(sysUser);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public int insertSysUsers(SysUsers sysUsers) {
        int count = 0;
        try {
            for (SysUser sysUser : sysUsers.getList()) {
                count += sysUserMapper.add(sysUser);
                //System.out.println(1 / 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return count;
    }
}
